package com.goodwill.androidtest.list_test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    public static void main(String[] args) {
//        ArrayList<City> sourceList = new ArrayList<City>(5);
//        List<City> cacheList = new ArrayList<City>(5);
//        List<City> targetList = new ArrayList<City>(5);
//        for (int i = 0; i < 5; i++) {
//            City city = new City();
//            city.setAddress("地址："+i);
//            city.setName("名称："+i);
//            sourceList.add(city);
//        }
//
////        cacheList = (List<City>) sourceList.clone();
//
//        Iterator<City> srcIterator = sourceList.iterator();
//        while (srcIterator.hasNext()) {
//            targetList.add(srcIterator.next().clone());
//        }
//
//        City city = targetList.get(2);
//        city.setName("北京");
//
//
//        System.out.println("源数据集："+sourceList);
//        System.out.println("缓存数据集："+cacheList);
//        System.out.println("目标数据集："+targetList);
        deepListTest();

    }

    private static void deepListTest() {
        List<Province> srcList = new ArrayList<>();
        List<Province> tarList = new ArrayList<>();
        Province province = new Province("山西");
        List<City> sxCityList = new ArrayList<>();
        City ty = new City("太原");
        City cz = new City("长治");
        City ll = new City("吕梁");
        sxCityList.add(ty);
        sxCityList.add(cz);
        sxCityList.add(ll);
        srcList.add(province);
        province.setCityList(sxCityList);
//        for (Province pro : srcList) {
//            tarList.add((Province) pro.clone());
//        }

        try {
            tarList.addAll(deepCopy(srcList));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        tarList.get(0).getCityList().get(1).setAddress("山西省长治市长子县");

        System.out.println("源数据集：" + srcList);
        System.out.println("目标数据集：" + tarList);
        System.out.println("-----------------------------------------------------------");

        try {
            tarList.clear();
            tarList.addAll(deepCopy(srcList));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("源数据集：" + srcList);
        System.out.println("目标数据集：" + tarList);
    }

    //关键代码 执行序列化和反序列化  进行深度拷贝
    public static <T> Collection<T> deepCopy(Collection<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);

        Collection<T> dest = (Collection<T>) in.readObject();
        return dest;
    }
}
